package com.zbkj.service.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zbkj.common.exception.CrmebException;
import com.zbkj.common.model.activity.SportfolioMarketActivity;
import com.zbkj.common.model.activity.SfullDiscountProduct;
import com.zbkj.common.model.product.Product;
import com.zbkj.common.page.CommonPage;
import com.zbkj.common.request.PageParamRequest;
import com.zbkj.common.request.merchant.MerchantSfullDiscountSearchRequest;
import com.zbkj.common.request.merchant.MerchantSportfolioMarketSearchRequest;
import com.zbkj.service.dao.MerchantSportfolioMarketActivityDao;
import com.zbkj.service.dao.MerchantSfullDiscountProductDao;
import com.zbkj.service.service.MerchantSportfolioMarketActivityService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * MerchantSportfolioMarketActivityServiceImpl 接口实现
 * +----------------------------------------------------------------------
 * | CRMEB [ CRMEB赋能开发者，助力企业发展 ]
 * +----------------------------------------------------------------------
 * | Copyright (c) 2016~2023 https://www.crmeb.com All rights reserved.
 * +----------------------------------------------------------------------
 * | Licensed CRMEB并不是自由软件，未经许可不能去掉CRMEB相关版权
 * +----------------------------------------------------------------------
 * | Author: CRMEB Team <admin@crmeb.com>
 * +----------------------------------------------------------------------
 */
@Service
public class MerchantSportfolioMarketActivityServiceImpl extends ServiceImpl<MerchantSportfolioMarketActivityDao, SportfolioMarketActivity> implements MerchantSportfolioMarketActivityService {

    @Resource
    private MerchantSportfolioMarketActivityDao dao;

    /**
     * 商户组合营销列表
     * @param request 请求参数
     * @return List
     */
    @Override
    public PageInfo<SportfolioMarketActivity> findPage(MerchantSportfolioMarketSearchRequest request, PageParamRequest pageParamRequest) {
        LambdaQueryWrapper<SportfolioMarketActivity> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(request.getMerId()), SportfolioMarketActivity::getMerId, request.getMerId());
        lqw.eq(Objects.nonNull(request.getActiveType()), SportfolioMarketActivity::getActiveType, request.getActiveType());
        lqw.eq(Objects.nonNull(request.getIsOpen()), SportfolioMarketActivity::getIsOpen, request.getIsOpen());
        lqw.eq(Objects.nonNull(request.getStatus()), SportfolioMarketActivity::getStatus, request.getStatus());
        lqw.eq(SportfolioMarketActivity::getIsDel, false);
        List<SportfolioMarketActivity> activityList = dao.selectList(lqw);
        Page<Product> page = PageHelper.startPage(pageParamRequest.getPage(), pageParamRequest.getLimit());
        return CommonPage.copyPageInfo(page, activityList);
    }

    @Override
    public List<SportfolioMarketActivity> findAllist(MerchantSportfolioMarketSearchRequest request) {
        LambdaQueryWrapper<SportfolioMarketActivity> lqw = Wrappers.lambdaQuery();
        lqw.eq(Objects.nonNull(request.getMerId()), SportfolioMarketActivity::getMerId, request.getMerId());
        lqw.eq(Objects.nonNull(request.getActiveType()), SportfolioMarketActivity::getActiveType, request.getActiveType());
        lqw.eq(Objects.nonNull(request.getIsOpen()), SportfolioMarketActivity::getIsOpen, request.getIsOpen());
        lqw.eq(Objects.nonNull(request.getStatus()), SportfolioMarketActivity::getStatus, request.getStatus());
        lqw.eq(Objects.nonNull(request.getProductId()), SportfolioMarketActivity::getProductId, request.getProductId());
        lqw.eq(SportfolioMarketActivity::getIsDel, false);
        return dao.selectList(lqw);
    }

    @Override
    public SportfolioMarketActivity activityDetail(Integer id) {
        SportfolioMarketActivity activity = dao.selectById(id);
        return activity;
    }

    /**
     * 新增商户组合营销
     * @param activity 请求参数
     * @return 新增结果
     */
    @Override
    public Boolean add(SportfolioMarketActivity activity) {
        //判断这个商品是否配置给对应的营销
        LambdaQueryWrapper<SportfolioMarketActivity> lqw = Wrappers.lambdaQuery();
        lqw.eq(SportfolioMarketActivity::getMerId, activity.getMerId());
        lqw.eq(SportfolioMarketActivity::getActiveType, activity.getActiveType());
        lqw.eq(SportfolioMarketActivity::getProductId, activity.getProductId());
        lqw.eq(SportfolioMarketActivity::getIsDel, false);
        Integer count = dao.selectCount(lqw);
        if (count>0){
            throw new CrmebException("该商品已经配置过对应的营销活动");
        }
        return dao.insert(activity)>0 ? true:false;
    }

    /**
     * 修改商户组合营销
     * @param activity 请求参数
     * @return 修改结果
     */
    @Override
    public Boolean updateSportfolioMarket(SportfolioMarketActivity activity) {
        //判断这个商品是否配置给对应的营销
        LambdaQueryWrapper<SportfolioMarketActivity> lqw = Wrappers.lambdaQuery();
        lqw.eq(SportfolioMarketActivity::getMerId, activity.getMerId());
        lqw.eq(SportfolioMarketActivity::getActiveType, activity.getActiveType());
        lqw.eq(SportfolioMarketActivity::getProductId, activity.getProductId());
        lqw.eq(SportfolioMarketActivity::getIsDel, false);
        Integer count = dao.selectCount(lqw);
        if (count>0){
            throw new CrmebException("该商品已经配置过对应的营销活动");
        }
        return dao.updateById(activity)>0 ? true:false;
    }

    /**
     * 删除商户组合营销
     * @param id 商户组合营销ID
     * @return 删除结果
     */
    @Override
    public Boolean delete(Integer id) {
        return dao.deleteById(id)>0 ? true:false;
    }
}

